package com.huluobo.a2107mvpimdemo.register.view;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.huluobo.a2107mvpimdemo.R;
import com.huluobo.a2107mvpimdemo.base.BaseActivity;
import com.huluobo.a2107mvpimdemo.register.presenter.RegisterContract;
import com.huluobo.a2107mvpimdemo.register.presenter.RegisterPresenter;

public class RegisterActivity extends BaseActivity<RegisterContract.View, RegisterPresenter> implements RegisterContract.View {

    private EditText username, password, rePassword;

    private Button registerBtn;

    @Override
    protected void initData() {
        presenter = new RegisterPresenter();
    }

    @Override
    protected void initView() {
        username = findViewById(R.id.register_username);
        password = findViewById(R.id.register_password);
        rePassword = findViewById(R.id.register_rePassword);

        registerBtn = findViewById(R.id.register_btn);

        registerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.register(username.getText().toString(), password.getText().toString(),
                        rePassword.getText().toString());
            }
        });
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void onRegisterSuccess() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                dismissProgressDialog();
                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }

    @Override
    public void onRegisterFailed(String error) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                dismissProgressDialog();
                Toast.makeText(RegisterActivity.this, "注册失败:"+error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onStartRegister() {
        showProgressDialog("开始注册");
    }

    @Override
    public void onUsernameError() {
        username.setError("用户名需要3-20位,并且首字符为英文");
    }

    @Override
    public void onPasswordError() {
        password.setError("密码需要3-20位的数字");
    }

    @Override
    public void onRePasswordError() {
        rePassword.setError("2次输入密码不一致");
    }
}